package example;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
 
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
 
public class IndexBuilder {
 
	static final String LUCENE_INDEX_DIRECTORY = "C:\\lucene";
	static final String DB_HOST_NAME = "localhost";
	static final String DB_USER_NAME = "root";
	static final String DB_PASSWORD = "jinoy";
 
	//method for indexing
	public void buildIndex(){
 
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		IndexWriter writer=null;
		StandardAnalyzer analyzer = null;		
		File file = null;
		try{
			System.out.println("Start indexing");
			//get a reference to index directory file
			file = new File(LUCENE_INDEX_DIRECTORY);
			analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
			writer = new IndexWriter(
					FSDirectory.open(file),
					analyzer,
					true,
					IndexWriter.MaxFieldLength.LIMITED
					);
 
 
			//initialize the driver class
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			//get connection object
			con = DriverManager.getConnection(
					"jdbc:mysql://"+DB_HOST_NAME+"/mydb",
					DB_USER_NAME, DB_PASSWORD);
			//create statement object
			stmt = con.createStatement();
			//execute query
			rs = stmt.executeQuery("SELECT * FROM employee");
			//iterate through result set
			while(rs.next()){
				String id = rs.getString("employeeid");
				String name = rs.getString("name");
				String age = rs.getString("age");
				String designation = rs.getString("designation");
				//create a full text field which contains name,
				//age and designation
				String fulltext = name + " " + age +
				" " + designation;
 
				//create document object
				Document document = new Document();
				//create field objects and add to document				
				Field idField = new Field("employeeid", 
						id, Field.Store.YES,
						Field.Index.NO);
				document.add(idField);
				Field nameField = new Field("name",
						name, Field.Store.YES,
						Field.Index.ANALYZED);
				document.add(nameField);
				Field ageField = new Field("age",
						age, Field.Store.YES,
						Field.Index.NOT_ANALYZED);
				document.add(ageField);
				Field designationField = new Field("designation",
						designation, Field.Store.YES,
						Field.Index.ANALYZED);
				document.add(designationField);
				Field fulltextField = new Field("fulltext",
						fulltext, Field.Store.NO,
						Field.Index.ANALYZED);
				document.add(fulltextField);
				//add the document to writer
				writer.addDocument(document);
			}
			//optimize the index
			System.out.println("Optimizing index");
			writer.optimize();
 
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(writer!=null)
					writer.close();
				if(rs!=null)
					rs.close();
				if(stmt!=null)
					stmt.close();
				if(con!=null)
					con.close();
				System.out.println("Finished indexing");
 
			}catch(Exception ex){
				ex.printStackTrace();
			}
 
		}
 
 
	}
 
	public static void main(String[] args) throws Exception {
 
		IndexBuilder builder = new IndexBuilder();
		builder.buildIndex();
	}
 
}